; example2.nsi
;
; This script is based on example1.nsi, but it remember the directory, 
; has uninstall support and (optionally) installs start menu shortcuts.
;
; It will install example2.nsi into a directory that the user selects,

;--------------------------------

!include "ZipDLL.nsh"
!include "EnvVar.nsh"
;!include "inetc_local.nsh"
!define PYTHON_URL "http://www.python.org/ftp/python/2.5.2/python-2.5.2.msi"

Function GetPython
    MessageBox MB_OK "The MLMMS installer will now install Python."
    StrCpy $2 "$TEMP\python-2.5.2.msi"
    nsisdl::download /TIMEOUT=30000 ${PYTHON_URL} $2
    Pop $R0 ;Get the return value
    	StrCmp $R0 "success" +3
      MessageBox MB_OK "Download failed: $R0"
      Quit
    ExecWait '"msiexec" /i $2'
    Delete $2
FunctionEnd

!define GDAL_URL "http://download.osgeo.org/gdal/win32/1.5/gdalwin32exe150.zip"

Function GetGDAL
    MessageBox MB_OK "The MLMMS installer will now install GDAL."
    StrCpy $2 "$TEMP\gdalwin32exe150.zip"
    nsisdl::download /TIMEOUT=30000 ${GDAL_URL} $2
    Pop $R0 ;Get the return value
    	StrCmp $R0 "success" +3
      MessageBox MB_OK "Download failed: $R0"
      Quit
    !insertmacro ZIPDLL_EXTRACT $2 "c:\" "<ALL>"
    Delete $2


   Push "PATH"
   Push "c:\gdalwin32-1.5\bin"
   Call AddToEnvVar
 
   Push "GDAL_DATA"
   Push "c:\gdalwin32-1.5\data"
   Call AddToEnvVar
FunctionEnd

!define GDALPYTHON_URL "http://pypi.python.org/packages/2.5/G/GDAL/GDAL-1.5.0.win32-py2.5.exe#md5=bed0134d414c9787dfea64dbcf17ee7a"

Function GetGDALPython
    MessageBox MB_OK "The MLMMS installer will now install GDAL python."
    StrCpy $2 "$TEMP\GDAL-1.5.0.win32-py2.5.exe"
    nsisdl::download /TIMEOUT=30000 ${GDALPYTHON_URL} $2
    Pop $R0 ;Get the return value
    StrCmp $R0 "success" +3
	MessageBox MB_OK "Download failed: $R0"
        Quit
    ExecWait $2
    Delete $2
FunctionEnd

!define FOURSUITE_URL "ftp://ftp.4suite.org/pub/4Suite/4Suite-XML-1.0.2.win32-py2.5.exe"

Function Get4Suite
    MessageBox MB_OK "The MLMMS installer will now install 4Suite."
    StrCpy $2 "$TEMP\4Suite-XML-1.0.2.win32-py2.5.exe"
;    nsisdl::download /TIMEOUT=30000 ${FOURSUITE_URL} $2
     inetc::get ${FOURSUITE_URL} $2
    ExecWait $2
    Delete $2
FunctionEnd

!define AMARA_URL "http://platea.pntic.mec.es/~jmorilla/amara/distros/Amara-1.2.0.2.win32-py2.5.exe"

Function GetAmara
    MessageBox MB_OK "The MLMMS installer will now install Amara."
    StrCpy $2 "$TEMP\Amara-1.2.0.2.win32-py2.5.exe"
     inetc::get ${AMARA_URL} $2
    ExecWait $2
    Delete $2
FunctionEnd

!define MYSQLPYTHON_URL "http://internap.dl.sourceforge.net/sourceforge/mysql-python/MySQL-python-1.2.2.win32-py2.5.exe"

Function GetMySQLPython
    MessageBox MB_OK "The MLMMS installer will now install MySQL Python."
    StrCpy $2 "$TEMP\MySQL-python-1.2.2.win32-py2.5.exe"
     inetc::get ${MYSQLPYTHON_URL} $2
    ExecWait $2
    Delete $2
FunctionEnd

!define MRSIDDECODER_URL "http://www.lizardtech.com/download/files/win/mrsiddecode_win.zip"
Function GetMrSidDecode
    MessageBox MB_OK "The MLMMS installer will now install the MrSid decoder."
    StrCpy $2 "$TEMP\mrsiddecode_win.zip"
    nsisdl::download /TIMEOUT=30000 ${MRSIDDECODER_URL} $2
    Pop $R0 ;Get the return value
     	StrCmp $R0 "success" +3
      MessageBox MB_OK "Download failed: $R0"
      Quit
    !insertmacro ZIPDLL_EXTRACT $2 "$INSTDIR" "<ALL>"
    Delete $2
FunctionEnd

!define PIL_URL "http://effbot.org/downloads/PIL-1.1.6.win32-py2.5.exe"

Function GetPIL
    MessageBox MB_OK "The MLMMS installer will now install PIL."
    StrCpy $2 "$TEMP\PIL-1.1.6.win32-py2.5.exe"
     inetc::get ${PIL_URL} $2
    ExecWait $2
    Delete $2
FunctionEnd

!define NUMPY_URL "http://voxel.dl.sourceforge.net/sourceforge/numpy/numpy-1.1.0-win32-superpack-python2.5.exe"

Function GetNumPy
    MessageBox MB_OK "The MLMMS installer will now install NumPy."
    StrCpy $2 "$TEMP\numpy-1.1.0-win32-superpack-python2.5.exe"
     inetc::get ${NUMPY_URL} $2
    ExecWait $2
    Delete $2
FunctionEnd

; The name of the installer
Name "MLMMS"

; The file to write
OutFile "mlmms.exe"

; The default installation directory
InstallDir $PROGRAMFILES\MLMMS

; Registry key to check for directory (so if you install again, it will 
; overwrite the old one automatically)
InstallDirRegKey HKLM "Software\MLMMS" "Install_Dir"

; Request application privileges for Windows Vista
RequestExecutionLevel admin

;--------------------------------

; Pages

Page components
Page directory
Page instfiles

UninstPage uninstConfirm
UninstPage instfiles

;--------------------------------

; The stuff to install
Section "MLMMS (required)"

  ; Install Python
;  Call GetPython
;  Call GetGDAL
;  Call GetGDALPython
;  Call Get4Suite
;  Call GetAmara
;  Call GetMySQLPython
  Call GetMrSidDecode
;  Call GetPIL
;  Call GetNumPy

  SectionIn RO
  
  ; Set output path to the installation directory.
  SetOutPath $INSTDIR
  
  ; Put file there
  File "dist/*"
;  File "fgdc_mysql.py"
;  File "gdal2tiles.py"
  
  ; Write the installation path into the registry
  WriteRegStr HKLM SOFTWARE\MLMMS "Install_Dir" "$INSTDIR"
  
  ; Write the uninstall keys for Windows
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MLMMS" "DisplayName" "MLMMS"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MLMMS" "UninstallString" '"$INSTDIR\uninstall.exe"'
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MLMMS" "NoModify" 1
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MLMMS" "NoRepair" 1
  WriteUninstaller "uninstall.exe"
  
SectionEnd

; Optional section (can be disabled by the user)
Section "Start Menu Shortcuts"

  CreateDirectory "$SMPROGRAMS\MLMMS"
  CreateShortCut "$SMPROGRAMS\MLMMS\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
  ;CreateShortCut "$SMPROGRAMS\MLMMS\MLMMS (MakeNSISW).lnk" "$INSTDIR\mlmms.nsi" "" "$INSTDIR\mlmms.nsi" 0
  
SectionEnd

;--------------------------------

; Uninstaller

Section "Uninstall"
  
  ; Remove registry keys
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MLMMS"
  DeleteRegKey HKLM SOFTWARE\MLMMS

  ; Remove files and uninstaller
  Delete $INSTDIR\*.py
  Delete $INSTDIR\uninstall.exe

  ; Remove shortcuts, if any
  Delete "$SMPROGRAMS\MLMMS\*.*"

  ; Remove directories used
  RMDir "$SMPROGRAMS\MLMMS"
  RMDir "$INSTDIR"

SectionEnd
